package com.eenie.pwdharck;

import android.os.Build;
import android.view.View;

import com.eenie.common.base.BaseActivity;
import com.eenie.common.subscribes.ProgressSubscriber;
import com.rx2androidnetworking.Rx2AndroidNetworking;
import com.yeahka.android.lepos.Device;
import com.yeahka.android.lepos.DeviceInfo;

import java.io.IOException;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class Main2Activity extends BaseActivity {


    @Override
    public int initContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void initInjector() {

    }

    @Override
    public void initUiAndListener() {

        findViewById(R.id.btn_encode)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        checkConfig();
                    }
                });
        findViewById(R.id.btn_login)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        login();
                    }
                });

    }


    private void login() {

//        E7890A9ADA5808DFCDC31F964CC4DEC5


        String str = loginParams(1, "18651736251", PwdEncodeUtil.encode("59420ppP").toUpperCase(), "");


        System.out.println("p = " + str);


//        Rx2AndroidNetworking.get("https://mobilepos.leshuazf.com/cgi-bin/lepos_proxy.cgi")
//                .addQueryParameter("p", str)
//                .build()
//                .getStringObservable()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ProgressSubscriber<String>() {
//                    @Override
//                    public void onNext(String s) {
//                        System.out.println("json = " + decodeJson(s));
//                    }
//
//                    @Override
//                    public void onFinish() {
//
//                    }
//                });


    }


    private String loginParams(int loginType, String account, String pwd, String code) {
        String valueOf = String.valueOf(System.nanoTime());
        switch (loginType) {
            case 1:
                String sign = sign(String.format("nonce=%s&operation=Login&password=%s&user_name=%s", valueOf, pwd, account));

                return loginEncode(new LoginParamsReqBean(valueOf, sign, account, pwd, null).toJson());
            case 2:
                String c2 = sign(String.format("nonce=%s&operation=Login&user_name=%s&vertify_code=%s", valueOf, account, code));

                return loginEncode(new LoginParamsReqBean(valueOf, c2, account, null, code).toJson());
            case 3:
                String c3 = sign(String.format("nonce=%s&operation=Login&password=%s&user_name=%s&vertify_code=%s", valueOf, pwd, account, code));

                return loginEncode(new LoginParamsReqBean(valueOf, c3, account, pwd, code).toJson());
            default:
                return "";
        }
    }


    private String sign(String str) {
        return new String(Device.nativeFunction20004(Device.getInstance(this).getDevice(), str));
    }


    private String loginEncode(String str) {
        int deviceInt = Device.getInstance(this).getDevice();


        byte[] nativeFunction60 = Device.nativeFunction60(deviceInt, Device.nativeFunction20003(deviceInt), configEncode(Device.nativeFunction66(deviceInt)), Device.nativeFunction20005(deviceInt, str));
        String sign = EncodeUtil.encode(nativeFunction60, 0, nativeFunction60.length);

        System.out.println(String.format("deviceInt = %s\njson = %s\nsign = %s", deviceInt, str, sign));

        return sign;
    }


    private void checkConfig() {
        int deviceInt = Device.getInstance(this).getDevice();
        byte[] nativeFunction2016 = Device.nativeFunction2016(deviceInt);
        byte[] nativeFunction2017 = Device.nativeFunction2017(deviceInt, Build.MODEL, DeviceInfo.getDeviceId(mContext));
        byte[] nativeFunction61 = Device.nativeFunction61(deviceInt, nativeFunction2016, configEncode(nativeFunction2017.length), nativeFunction2017, new byte[1]);


        String str = EncodeUtil.encode(nativeFunction61, 0, nativeFunction61.length);


        Rx2AndroidNetworking.get("https://mobilepos.leshuazf.com/cgi-bin/lepos_proxy.cgi")
                .addQueryParameter("p", str)
                .build()
                .getStringObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        System.out.println("json = " + decodeJson(s));
                    }

                    @Override
                    public void onFinish() {

                    }
                });

    }

    public byte[] configEncode(int i) {
        return new byte[]{(byte) (i & 255), (byte) ((i >> 8) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 24) & 255)};
    }


    private String decodeJson(String src) {

        try {
            byte[] json = EncodeUtil.decode(src);
            return new String(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }


}
